package com.jzlin.funhub.admin.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jzlin.funhub.admin.model.dto.QuestionAnswerPageDTO;
import com.jzlin.funhub.admin.model.dto.QuestionAnswerRepliesPageDTO;
import com.jzlin.funhub.admin.model.dto.QuestionPageDTO;
import com.jzlin.funhub.admin.model.req.*;
import com.jzlin.funhub.admin.model.resp.QuestionDetailResp;
import com.jzlin.funhub.admin.service.question.QuestionAnswerRepliesService;
import com.jzlin.funhub.admin.service.question.QuestionAnswerService;
import com.jzlin.funhub.admin.service.question.QuestionCategoryService;
import com.jzlin.funhub.admin.service.question.QuestionService;
import com.jzlin.funhub.framework.core.http.RestRet;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 问答广场控制器.
 *
 * @author linjz
 * @since 2025/5/23
 */
@Api(tags = {"问答广场"})
@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;
    @Autowired
    private QuestionCategoryService questionCategoryService;
    @Autowired
    private QuestionAnswerService questionAnswerService;
    @Autowired
    private QuestionAnswerRepliesService questionAnswerRepliesService;


    @ApiOperation("查询问题分类树")
    @GetMapping("/category/tree")
    public RestRet<List<Tree<Long>>> queryCategoryTree() {
        return RestRet.createSuccessRet(questionCategoryService.queryTree());
    }

    @ApiOperation("分页查询问题列表")
    @PostMapping("/page")
    public RestRet<IPage<QuestionPageDTO>> pageQuestion(@RequestBody @Valid QuestionPageReq req) {
        return RestRet.createSuccessRet(questionService.queryPage(req));
    }

    @ApiOperation("查询问题详情")
    @GetMapping("/detail")
    public RestRet<QuestionDetailResp> getDetail(Long id) {
        return RestRet.createSuccessRet(questionService.getDetail(id));
    }

    @ApiOperation("分页查询问题回答列表")
    @PostMapping("/answer/page")
    public RestRet<IPage<QuestionAnswerPageDTO>> pageQuestionAnswer(@RequestBody @Valid QuestionAnswerPageReq req) {
        return RestRet.createSuccessRet(questionAnswerService.queryPage(req));
    }

    @ApiOperation("分页查询问题回答嵌套列表")
    @PostMapping("/answer/replies/page")
    public RestRet<IPage<QuestionAnswerRepliesPageDTO>> pageQuestionAnswerReplies(@RequestBody QuestionAnswerRepliesPageReq req) {
        return RestRet.createSuccessRet(questionAnswerRepliesService.queryPage(req));
    }

    @ApiOperation("保存问题草稿")
    @PostMapping("/saveDraft")
    public RestRet<Long> saveDraftQuestion(@RequestBody @Valid QuestionSaveDraftReq req) {
        return RestRet.createSuccessRet(questionService.saveDraft(req));
    }

    @ApiOperation("发布新问题")
    @PostMapping("/publish")
    public RestRet<Long> publishQuestion(@RequestBody @Valid QuestionPublishReq req) {
        return RestRet.createSuccessRet(questionService.publishNewQuestion(req));
    }

    @ApiOperation("发布问题回答")
    @PostMapping("/answer/publish")
    public RestRet<Void> publishAnswer(@RequestBody @Valid QuestionAnswerPublishReq req) {
        questionAnswerService.publishNewAnswer(req);
        return RestRet.createSuccessRet();
    }
}
